package query

import (
	"strings"

	"ssdevice/common/dto"
)

type SysProductQuery struct {
	dto.Pagination `search:"-"`
	dto.OrderBy    `search:"-"`
	dto.UserInfo
	Id             string   `form:"id" json:"id" search:"type:exact;column:id;table:sys_dev_product"`
	Name           string   `form:"name" search:"type:contains;column:name;table:sys_dev_product"`
	ModelSearch    string   `form:"model" search:"type:contains;column:model;table:sys_dev_product"`
	DeviceType     string   `form:"deviceType" search:"type:exact;column:device_type;table:sys_dev_product"`
	DeviceTypeNeq  string   `form:"deviceTypeNe" search:"type:ne;column:device_type;table:sys_dev_product"`
	DeptId         string   `form:"deptId" search:"-"`
	DeptIdNot      string   `form:"deptIdNot" search:"-"`
	Status         *bool    `form:"status" search:"-"`
	ProductModel   string   `form:"productModel" search:"type:exact;column:model;table:sys_dev_product"`
	ProductModelIn []string `form:"productModelIn" search:"type:in;column:model;table:sys_dev_product"`
	ProductModels  string   `form:"productModels" search:"-"`

	IdIn            []string `search:"type:in;column:id;table:sys_dev_product"`
	NameEq          string   `search:"type:exact;column:name;table:sys_dev_product"`
	DeptIdIn        []string `search:"type:in;column:dept_id;table:sys_dev_product"`
	DeptIdNIn       []string `search:"type:nin;column:dept_id;table:sys_dev_product"`
	ProductModelNIn []string `search:"type:nin;column:model;table:sys_dev_product"`
	NotRelease      bool     `search:"type:ne;column:status;table:sys_dev_product"`
	Release         bool     `search:"type:exact;column:status;table:sys_dev_product"`
}

func (m *SysProductQuery) GetNeedSearch() interface{} {
	if len(m.DeptId) == 0 {
		if m.UserId != "1" {
			m.DeptIdIn = m.DeptIdList
		}
	} else {
		m.DeptIdIn = strings.Split(strings.ReplaceAll(m.DeptId, " ", ""), ",")
	}
	if m.Status != nil {
		if *m.Status {
			m.Release = true
		} else {
			m.NotRelease = true
		}
	}
	if len(m.DeptIdNot) > 0 {
		m.DeptIdNIn = strings.Split(strings.ReplaceAll(m.DeptIdNot, " ", ""), ",")
	}
	if len(m.ProductModels) > 0 {
		m.ProductModelIn = strings.Split(strings.ReplaceAll(m.ProductModels, " ", ""), ",")
	}
	return *m
}
